package com.qf.lanzhou.shop.api.controller;

import com.qf.lanzhou.shop.api.service.impl.ReceivingAddressServiceImpl;
import com.qf.lanzhou.shop.pojo.entity.ReceivingAddress;
import com.qf.lanzhou.shop.pojo.entity.User;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author jack
 */
@Api(value = "收货地址接口")
@RestController()
public class ReceivingAddressController extends BaseController {
    @Autowired
    private ReceivingAddressServiceImpl rasi;

    @PostMapping(value = "/addAddress")
    @ApiOperation(value = "新增收货地址")
    public BaseRespVO addAddress( ReceivingAddress receivingAddress,HttpServletRequest req) {
        System.out.println("增加收货地址");
        System.out.println(receivingAddress);
        User user = (User) req.getSession().getAttribute("user");

        receivingAddress.setUserId(user.getId());
        receivingAddress.setDefaultState(0L);
        boolean b = rasi.addAddress(receivingAddress);
        return getBaseRespVO(b);
    }

    @PostMapping(value = "/updateAddress")
    @ApiOperation(value = "修改收货地址")
    public BaseRespVO updateAddress( ReceivingAddress receivingAddress) {
        System.out.println("修改收货地址： "+receivingAddress);
        boolean b = rasi.updateAddress(receivingAddress);
        return getBaseRespVO(b);
    }

    @GetMapping(value = "/delectAddress")
    @ApiOperation(value = "删除收货地址")
    public BaseRespVO delectAddress(Long addressId) {
        boolean b = rasi.delectAddress(addressId);
        return getBaseRespVO(b);
    }
    @GetMapping(value = "/selectOneAddress")
    @ApiOperation(value = "获取一条收货地址")
    public BaseRespVO modificationAddress(Long addressId) {
        ReceivingAddress address = rasi.getAddressById(addressId);
        return convertSuccessResult(address);
    }

    @GetMapping(value = "/userAllAddress")
    @ApiOperation(value = "获得买家所有收货地址")
    public BaseRespVO userAllAddress(HttpServletRequest req) {
        System.out.println("获取买家收货地址");
        User user = (User) req.getSession().getAttribute("user");
        List<ReceivingAddress> addressList = rasi.getAddressByUserId(user.getId());
        return convertSuccessResult(addressList);
    }

    @ApiOperation(value = "设置默认地址")
    @GetMapping(value = "/setAsDefault")
    public BaseRespVO setasdefault(Long addressId, HttpServletRequest req) {
        //先把原来的状态值设置为0
        System.out.println("进入后端设置默认");
        User user = (User) req.getSession().getAttribute("user");
        boolean b = rasi.forZero(user.getId());
        //再把现在的设置为1
        boolean b1 = rasi.forOne(addressId);
        return getBaseRespVO(b1);
    }

    private BaseRespVO getBaseRespVO(boolean b) {
        if (b) {
            return convertErrorResult("系统繁忙");
        }
        return convertSuccessResult("成功啦");
    }

}
